package com.project.service.imp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.CommodityBean;
import com.project.mapper.ICommodityMapper;
import com.project.service.ICommodityService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class CommodityServiceImp extends BaseService implements ICommodityService {
    @Override
    public void add(CommodityBean commodity) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        mapper.add(commodity);
        session.commit();
        session.close();
    }

    @Override
    public void delById(Integer id) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        mapper.delById(id);
        session.commit();
        session.close();
    }

    @Override
    public void updById(Integer id, Double money) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        mapper.updById(id,money);
        session.commit();
        session.close();
    }

    @Override
    public CommodityBean findById(Integer id) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        CommodityBean comBean= mapper.findById(id);
        session.close();
        return comBean;
    }

    @Override
    public PageInfo<CommodityBean> cutByItem(Integer pageNum, String name, Double MaxMoney, Double MinMoney) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        //设置 当前页面 和 每页显示的记录数
        PageHelper.startPage(pageNum,3);
        PageInfo<CommodityBean> pg =PageInfo.of(mapper.cutByItem(name,MaxMoney,MinMoney));
        session.close();
        return pg;
    }

    @Override
    public PageInfo<CommodityBean> cutByFactory(Integer pageNum, String factory, LocalDate startTime, LocalDate endTime) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        //设置 当前页面 和 每页显示的记录数
        PageHelper.startPage(pageNum,3);
        PageInfo<CommodityBean> pg =PageInfo.of(mapper.cutByFactory(factory,startTime,endTime));
        session.close();
        return pg;
    }

    @Override
    public void addList(List<CommodityBean> list) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        mapper.addList(list);
        session.commit();
        session.close();
    }
    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        ICommodityMapper mapper = session.getMapper(ICommodityMapper.class);
        mapper.delMore(idArray);
        session.commit();
        session.close();
    }
}
